from apscheduler.schedulers.blocking import BlockingScheduler
from collector import WeatherDataCollector
from loadweather import load_weather_data
import configparser
import logging


def load_config():
    config = configparser.ConfigParser()
    config.read('config.ini')
    return config


def collect_and_load_data():
    try:
        # 读取配置
        config = load_config()
        api_key = config['API']['key']
        locations = config['Locations']['cities'].split(',')

        # 收集数据
        collector = WeatherDataCollector(api_key, locations)
        weather_df = collector.collect_weather_data()

        # 保存为临时CSV文件
        temp_file = 'temp_weather_data.csv'
        weather_df.to_csv(temp_file, index=False)

        # 导入数据到Hive
        load_weather_data(temp_file)

        logging.info("Successfully completed data collection and loading")

    except Exception as e:
        logging.error(f"Error in data collection and loading process: {str(e)}")


def main():
    scheduler = BlockingScheduler()

    # 设置每小时执行一次
    scheduler.add_job(collect_and_load_data, 'interval', hours=1)

    logging.info("Starting weather data collection scheduler")
    scheduler.start()


if __name__ == "__main__":
    main()